import Vue from 'vue'
// import {Toast} from 'vant'
// Vue.use(Toast)
// Tab选项卡固定指定
Vue.directive('title', {
    // 插入到DOM的钩子函数
    inserted (el, bind) {
      window.onscroll = () => {
        if ((document.documentElement.scrollTop || document.body.scrollTop) > bind.value) {
          // 显示
          el.style.position = 'fixed'
          el.style.top = 0
          el.style.zIndex = '10000'
          el.style.width = '100%'
        } else {
          // 隐藏
          el.style.position = 'static'
        }
      }
    }
})
//

Vue.directive('toast',(el, binding)=>{
    const toast = Vue.$toast({
        msg: binding.value.msg ||'默认msg',
        duration: binding.value.time,
        closeOnClick: true,

    })
    if(binding.value){
        let i = binding.value.time
        const timer = setInterval(()=>{
            i--;
            if (i<=0){
                clearInterval(timer)
                toast.clear()
            }
        },1000)
    }
})
Vue.directive('gotop', {
    inserted(el, binding) {
        let oImg = document.createElement('img');
        oImg.src = 'http://f0.jmstatic.com/btstatic/h5/index/go_top.png'
        el.append(oImg)
        el.scrTop = 0;
        el.offTop = 50;
        el.style.display = 'none'
        el.style.position = 'fixed';
        el.style.zIndex = '999';
        el.style.bottom =  '60px';
        el.style.height =  '40px';
        el.style.width =  '40px';
        el.style.right =  '20px';
        el.offTop = binding.value || 50; // 垂直滚动条滚动距离
        window.addEventListener('scroll', ()=>{
            el.scrTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
            if (el.scrTop > el.offTop) {
                el.style.display = 'block'
            } else {
                el.style.display = 'none'
            }
        })
        el.onclick = () => {
            let timer = setInterval(() => {
                let ispeed = Math.floor(-el.scrTop / 5)
                document.documentElement.scrollTop = document.body.scrollTop = el.scrTop + ispeed
                if (el.scrTop === 0) {
                    clearInterval(timer)
                }
            }, 16)
        }
    },
    unbind(){
        // window.removeEventListener('scroll')
    }
})

